<?php

namespace App\Modules\Post\Http\Controllers\Api;

use Illuminate\Http\Request;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
use App\Modules\Post\Models\Post;
use App\Modules\Post\Http\Resources\PostResource;
use App\Modules\Post\Http\Requests\PostCreateRequest;
use App\Modules\Post\Http\Requests\PostUpdateRequest;
use App\Http\Controllers\Controller;

class PostController extends Controller
{

    /**
     * @var Laoha\Modules
     */
    protected $module_slug = "post";



    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $limit    = (int)$request->get('limit',0);
        $per_page = (int)$request->get('per_page',0);

        //dd(date('y-m-d',$request->get('created')));
        $data = QueryBuilder::for(Post::class)
        ->allowedSorts('id','created_at')
        ->allowedFilters([
                AllowedFilter::exact('id'),
                AllowedFilter::exact('cate_id'),
                AllowedFilter::exact('user_id'),
                AllowedFilter::exact('group'),  //绝对相等
                AllowedFilter::scope('pos', 'pos'),
                AllowedFilter::scope('created_at', 'create_between'),
                'title',
                'content'
            ])
        ->where('status',1);
        if( $limit > 0 ){
            $data = $data->simplePaginate($limit);
        }elseif($per_page > 0){
            $data = $data->paginate($per_page);
        }else{
            $data = $data->paginate();   
        }
        $data = $data->appends(request()->query());   

        return json_ok($data->toArray());
	$data = Post::paginate(10);
	$data = PostResource::collection($data);
	
	return $data->additional(json_ext(['module'=>get_module($this->module_slug)])); 
	//
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Modules\Post\Http\Requests\PostCreateRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PostCreateRequest $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
	$data = Post::find($id);
	$data = new PostResource($data);
	
	return $data->additional(json_ext(['module'=>get_module($this->module_slug)])); 
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Modules\Post\Http\Requests\PostUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(PostUpdateRequest $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
